package simorg.actions;

import simorg.controller.UserController;
import simorg.domain.Role;
import simorg.domain.User;
import simorg.domain.persistence.UserTable;
import simorg.view.ModelView;
import simorg.view.WebSessionContext;

public class UserNew extends Action {

	public static String actionName = "new";
	public UserNew() {
		super(actionName);
		this.controllerName = UserController.controllerName;
	}
	public UserNew(String courseId) {
		super(actionName);
		this.controllerName = UserController.controllerName;
		this.getRequestArgs().add(courseId);
	}
	
	@Override
	public ModelView checkAccess(WebSessionContext wc) throws Exception {
		return this.checkAdminAccess(wc);
	}
	
	@Override
	public ModelView process(WebSessionContext wc) throws Exception {
		User u = new User();
		if ( wc.hasReqeustParameter("role") ) {
			u.setRole(Role.valueOf(wc.getRequestParameter("role")));
		}
		UserTable.save(wc.getDbConnection(), u);
		Action redirect = new UserView(String.valueOf(u.getId()));
		wc.redirectAdmin(redirect);
		return null;
	}

}
